import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class bluesun {

    private static String currentDirectory = System.getProperty("user.dir");

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.print(currentDirectory + "> ");
            String input = scanner.nextLine();

            String[] command = input.split(" ");
            String action = command[0];

            if (action.equals("cd")) {
                String path = command[1];
                if (path.equals("..")) {
                    currentDirectory = new File(currentDirectory).getParent();
                } else {
                    File newDirectory = new File(currentDirectory, path);
                    if (newDirectory.isDirectory()) {
                        currentDirectory = newDirectory.getAbsolutePath();
                    } else {
                        System.out.println("Error: Not a directory");
                    }
                }
            } else if (action.equals("touch")) {
                String filename = command[1];
                String filePath = currentDirectory + File.separator + filename;
                File file = new File(filePath);
                try {
                    if (file.createNewFile()) {
                        System.out.println("File created: " + filename);
                    } else {
                        System.out.println("Error: File already exists");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if (action.equals("delete")) {
                String filename = command[1];
                String filePath = currentDirectory + File.separator + filename;
                File file = new File(filePath);
                if (file.delete()) {
                    System.out.println("File deleted: " + filename);
                } else {
                    System.out.println("Error: Could not delete file");
                }
            } else if (action.equals("mkdir")) {
                String dirname = command[1];
                String dirPath = currentDirectory + File.separator + dirname;
                File dir = new File(dirPath);
                if (dir.mkdir()) {
                    System.out.println("Directory created: " + dirname);
                } else {
                    System.out.println("Error: Could not create directory");
                }
            } else if (action.equals("mv")) {
                String source = command[1];
                String destination = command[2];
                String sourcePath = currentDirectory + File.separator + source;
                String destPath = currentDirectory + File.separator + destination;
                File sourceFile = new File(sourcePath);
                File destFile = new File(destPath);
                if (sourceFile.renameTo(new File(destFile, sourceFile.getName()))) {
                    System.out.println("File moved: " + source + " to " + destination);
                } else {
                    System.out.println("Error: Could not move file");
                }
            } else if (action.equals("ls")) {
                File currentDir = new File(currentDirectory);
                String[] files = currentDir.list();
                for (String file : files) {
                    System.out.println(file);
                }
            } else if (action.equals("cat")) {
                String filename = command[1];
                String filePath = currentDirectory + File.separator + filename;
                File file = new File(filePath);
                try {
                    Scanner fileScanner = new Scanner(file);
                    while (fileScanner.hasNextLine()) {
                        System.out.println(fileScanner.nextLine());
                    }
                    fileScanner.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if (action.equals("write")) {
                String filename = command[1];
                String filePath = currentDirectory + File.separator + filename;
                File file = new File(filePath);
                try {
                    FileWriter writer = new FileWriter(file, true);
                    System.out.println("Enter text (press Ctrl+D to finish):");
                    Scanner textScanner = new Scanner(System.in);
                    while (textScanner.hasNextLine()) {
                        writer.write(textScanner.nextLine());
                        writer.write(System.lineSeparator());
                    }
                    writer.close();
                    textScanner.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("Error: Invalid command");
            }
        }
    }
}
